package divide;

import divide.ColumnDivider.DividingResult;

public class Printer {
    
    public static void printValue(int value){
        System.out.println(value);
    }
    
    public static void printColumn(DividingResult dividingResult){
        printFirstLine(dividingResult.getDividend(), dividingResult.getDivisor());
        printSecondLine(dividingResult.getDividend(), 
                dividingResult.getDivisor(), dividingResult.getResult());
        int[] intermidiateValueArray = dividingResult
                .getDividentIntermidiateArray();
        int[] dividentIntermidiateArray = dividingResult
                .getDividentIntermidiateArray();
        printSirdLine(dividingResult.getDividend(), intermidiateValueArray[0], 
                dividingResult.getResult());
        printElseColumn(dividentIntermidiateArray, intermidiateValueArray);
        printRest(dividingResult.getDividend(), dividingResult.getRest());
    }
    
    private static void printFirstLine(int dividend, int divisor){
        System.out.println(dividend + "|" + divisor); 
    }
    
    private static void printSecondLine(int dividend, int divisor, int result){
        countPrinter(String.valueOf(dividend).length(), " ");
        System.out.print("|");
        countPrinter(getLongerValueLength(divisor, result), "-");
        System.out.println();
    }
    
    private static void printSirdLine(int dividend, int firstIntermidiateValue,
            int result){
        System.out.print(firstIntermidiateValue);
        countPrinter((String.valueOf(dividend).length() - 
                String.valueOf(firstIntermidiateValue).length()), " ");
        System.out.print("|");
        System.out.println(result);
        countPrinter(String.valueOf(firstIntermidiateValue).length(), "-");
        System.out.println();
    }
    
    private static void printElseColumn (int[] dividentIntermidiateArray, 
            int[] intermidiateValueArray){
        int count = 1;
        while(dividentIntermidiateArray[count] != 0){            
            System.out.println(additionalLength(String.valueOf(
                    dividentIntermidiateArray[0]).length() + count - 1,
                    String.valueOf(dividentIntermidiateArray[count]).length())
                    + dividentIntermidiateArray[count]);
            System.out.println(additionalLength(String.valueOf(
                    intermidiateValueArray[0]).length() + count - 1, 
                    String.valueOf(intermidiateValueArray[count]).length())
                    + intermidiateValueArray[count]);
            System.out.print(additionalLength(String.valueOf(
                    dividentIntermidiateArray[0]).length() + count - 1, 
                    String.valueOf(dividentIntermidiateArray[count]).length()));
            countPrinter(getLongerValueLength(dividentIntermidiateArray[count], 
                    intermidiateValueArray[count]), "-");
            System.out.println();
            count++;
        }
    }
    
    private static void printRest(int divident, int rest){
        System.out.println(additionalLength(String.valueOf(divident).length() - 1, 
                String.valueOf(rest).length()) + rest);
    }
    
    private static void countPrinter(int count, String value){
        for(int i = 0; i < count; i++){
            System.out.print(value); 
        }
    }
    
    private static String additionalLength(int first, int second){
        StringBuilder whiteSpace = new StringBuilder();
        for(int i = 0; i <= first - second; i++){
            whiteSpace.append(" ");
        }
        return whiteSpace.toString();
    }
    
    private static int getLongerValueLength(int first, int second){
        int length;
        if(String.valueOf(first).length() < String.valueOf(second).length()){
            length = String.valueOf(second).length();
        }
        else{
            length = String.valueOf(first).length();
        }
        return length;
    }
    
    public static void printResult(int result, int rest){
        System.out.println();
        System.out.println("Dividing results:");
        System.out.println("result = " + result);
        System.out.println("rest = " + rest);
    }
}

